package view;

import controller.DeleteUserController;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class DeleteUserView {
    private final String username;
    private final DeleteUserController controller = new DeleteUserController();

    public DeleteUserView(String username) {
        this.username = username;
    }

    public void showUi() {
        JFrame jf = new JFrame("删除好友");
        jf.setIconImage(Toolkit.getDefaultToolkit().createImage(MainView.class.getResource("qq.jpg")));
        JPanel jp = new JPanel();
        JLabel jl1 = new JLabel("删除好友");
        JLabel jLabel = new JLabel("请选择需删除好友的账号昵称");
        String[] users = controller.getUsers(username);
        JComboBox<String> comboBox = new JComboBox<>(users);
        jLabel.setFont(new Font("黑体", Font.BOLD, 24));
        jl1.setFont(new Font("黑体", Font.BOLD, 24));
        JButton bu2 = new JButton("删    除");
        JButton bu3 = new JButton("退    出");
        jp.setLayout(null);
        jl1.setBounds(200, 1, 200, 50);
        jLabel.setBounds(100, 50, 400, 50);
        comboBox.setBounds(100, 100, 300, 50);
        bu2.setBounds(175, 200, 100, 50);
        bu3.setBounds(175, 300, 100, 50);
        jf.getRootPane().setDefaultButton(bu2);
        jp.add(jl1);
        jp.add(jLabel);
        jp.add(comboBox);
        jp.add(bu2);
        jp.add(bu3);
        jf.add(jp);
        jf.setVisible(true);
        jf.setBounds(600, 200, 500, 500);

        bu2.addActionListener(e -> {
            Object selectedItem = comboBox.getSelectedItem();
            boolean b = controller.deleteUser(username, (String) selectedItem);
            if (b) {
                JOptionPane.showMessageDialog(null, "删除成功！！！");
            } else {
                JOptionPane.showMessageDialog(null, "该用户已被删除！！！");
            }
        });
        bu3.addActionListener(e -> jf.dispose());
        jf.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                jf.dispose();
            }
        });
    }
}